package com.henghai.zhenxuan.ui.bind;


import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.databinding.BindingAdapter;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.ActivityUtils;
import com.henghai.zhenxuan.MainActivity;
import com.henghai.zhenxuan.data.config.AppConfig;
import com.henghai.zhenxuan.data.config.Const;
import com.henghai.zhenxuan.ui.page.MainFragment;
import com.henghai.zhenxuan.ui.view.text.VerticalScrollTextView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.henghai.zhenxuan.R;
import com.hjq.bar.TitleBar;
import com.just.agentweb.AgentWebView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;


public class FragmentBindingAdapter {
    //    public static final int NAVIGATION_HOME = R.id.navigation_home;
    public static final int NAVIGATION_RAISE = com.henghai.zhenxuan.R.id.navigation_raise;
//    public static final int NAVIGATION_MALL = R.id.navigation_mall;
//    public static final int NAVIGATION_SIGN_IN = R.id.navigation_sign_in;
public static final int NAVIGATION_ADOPT = R.id.navigation_adopt;
    public static final int NAVIGATION_MINE = R.id.navigation_mine;

    @BindingAdapter(value = {"bindingFragment"}, requireAll = false)
    public static void bindingFragment(BottomNavigationView navView, int id) {
        NavController navController = Navigation.findNavController(ActivityUtils.getTopActivity(), id);
        NavigationUI.setupWithNavController(navView, navController);
    }


//    @BindingAdapter(value = {"appType"}, requireAll = false)
//    public static void initAppType(View view, int type) {
//        if (type == Const.APP_TYPE_TYPE1 || type == Const.APP_TYPE_TYPE6) {
//            view.setVisibility(View.VISIBLE);
//        } else {
//            view.setVisibility(View.GONE);
//        }
//    }

    @BindingAdapter(value = {"appTabs"}, requireAll = false)
    public static void initAppTabs(View view, String tabs) {
        if (tabs.equals("1,2,3,4")) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    @BindingAdapter(value = {"bindTabMenu"}, requireAll = false)
    public static void initTabMenu(BottomNavigationView navigationView, String type) {
        Menu menu = navigationView.getMenu();
        Set<String> typeSet = new HashSet<>(Arrays.asList(type.split("")));
        for (int i = 1; i < 4; i++) {
            if (!typeSet.contains(String.valueOf(i))) {
                switch (i) {
                    case 1:
                        menu.findItem(NAVIGATION_RAISE).setVisible(false);
                        break;
                    case 2:
                        menu.findItem(NAVIGATION_ADOPT).setVisible(false);
                        break;
                    case 3:
                        menu.findItem(NAVIGATION_MINE).setVisible(false);
                        break;
//                    case 4:
//                        menu.findItem(NAVIGATION_SIGN_IN).setVisible(false);
//                        break;
                }
            }
        }
    }

    //底部菜单栏初始化及切换
    @BindingAdapter(value = {"bindTabAndPage"}, requireAll = false)
    public static void initTabAndPage(ViewPager2 viewPager, boolean initTabAndPage) {
        viewPager.setUserInputEnabled(initTabAndPage);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(new MainFragment.FragmentPagerAdapter((FragmentActivity) viewPager.getContext()));
        BottomNavigationView bottomNavigationView = (viewPager.getRootView()).findViewById(R.id.bottom_main);

        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
//            if (itemId == R.id.navigation_home) {
//                viewPager.setCurrentItem(0, false);
//            } else if (itemId == R.id.navigation_mall) {
//                viewPager.setCurrentItem(1, false);
//            } else if (itemId == R.id.navigation_sign_in) {
//                viewPager.setCurrentItem(2, false);
//            } else if (itemId == R.id.navigation_mine) {
//                viewPager.setCurrentItem(3, false);
//            }
            if (itemId == R.id.navigation_raise) {
                viewPager.setCurrentItem(0, false);
            } else if (itemId == R.id.navigation_adopt) {
                viewPager.setCurrentItem(1, false);
            }else if (itemId == R.id.navigation_mall){
                viewPager.setCurrentItem(2, false);
            }else if (itemId == R.id.navigation_mine) {
                viewPager.setCurrentItem(3, false);
            }
            if (AppConfig.INSTANCE.getInnerAdCount() < AppConfig.INSTANCE.getMConfigs().login().get().getPlacementTwoCount()) {
                Log.d("MainActivity-", "initTabAndPage:"+AppConfig.INSTANCE.getInnerAdCount()  +"  "+AppConfig.INSTANCE.getMConfigs().login().get().getPlacementTwoCount());
                    ((MainActivity) viewPager.getContext()).showInnerAd();
            }
            return true;
        });
        try {
//            int defaultTab = getFirstCharacter(AppConfig.INSTANCE.getMConfigs().login().get().getPackageTabs());
//            if (defaultTab == 1) {
//                bottomNavigationView.setSelectedItemId(R.id.navigation_adopt);
//            } else if (defaultTab == 2) {
//                bottomNavigationView.setSelectedItemId(R.id.navigation_mine);
//            }
//            else if (defaultTab == 3) {
//                bottomNavigationView.setSelectedItemId(R.id.navigation_sign_in);
//            }
        } catch (Exception e) {
            viewPager.setCurrentItem(0, false);
        }
    }

    public static int getFirstCharacter(String str) {
        if (str == null || str.isEmpty()) {
            return 0;
        }
        return Integer.parseInt(String.valueOf(str.charAt(0))) - 1;
    }

    @BindingAdapter(value = {"textAdapter"}, requireAll = false)
    public static void initTextAdapter(VerticalScrollTextView textView, ArrayList<String> list) {
        textView.setDataSource(list);
        textView.startPlay();
    }

    @BindingAdapter(value = {"titleBarText"}, requireAll = false)
    public static void setTitleBarText(TitleBar textView, String text) {
        textView.setTitle(text);
    }

    @BindingAdapter(value = {"webViewUrl"}, requireAll = false)
    public static void setWebView(AgentWebView webView, String text) {
        WebSettings mWebSettings = webView.getSettings();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebSettings.setJavaScriptEnabled(true);
        webView.loadUrl(text);
    }

}

